package clientGUI.generalPanels.Controllers;

import java.util.Vector;

import college.*;

import clientGUI.Controllers.Controller;
import clientserver.*;

/**
 * Control the user logging in system
 *  Futures:
 *  Login validation
 *  Passsword validation
 *  No more than one connection from same user at the same time
 * 
 * @author Maxim Rahman
 * 
 */

public class LoginController extends Controller {

	private String password;
	private String enteredpassword;
	private boolean loggedin = false;
	private boolean loginexist = false;
	private boolean multilogin=false;
	private User user;

	// Getters And setters/////////////////////////////////////

	public void setClient(Client cl) // overwrited Controller method

	{

		client = cl;
		user.setClient(cl);
	}

	public void SetPassword(String psw) {
		enteredpassword = psw;
	}

	public String getLogin() {
		return user.getLogin();
	}

	public void setLogin(String login) {
		user.setLogin(login);
	}

	public User getlogedUser() {
		
		
		return user;
	}


	// Constructor///////////////////////////////////////////

	public LoginController() {

		user = new User();
	}

	public void docheck() {

		loginexist = user.loadfromdb(); //  load all  user information from  Mysql DB

		if (loginexist) // if user login exist check password correctness
		{

			if (enteredpassword.equals(user.getPassword()))
			{
					loggedin = true;
				
					multilogin=user.isalreadylogged();
					user.setloggedstatus(true);
					user.updateloggedstatus(client);
			}
			else
				loggedin = false;  
		}


		
	} // docheck()

	

	public boolean isloggedin() {
		return loggedin;
	}

	/**
	 * check if users already logged in server
	 * @return true if user already logged in, otherwise false
	 */
	public boolean multilogin()
	{

		return multilogin;
	}
	public boolean correctlogin() {
		return loginexist;

	}
	
	
	
	public void logout()
	{
		user.setloggedstatus(false);
		user.updateloggedstatus(client);
		
	}
	
	
	}


